mui.init();
var old_back = mui.back;
mui.back = function(){
 // var btn = ["确定","取消"];
 // mui.confirm('确认关闭当前窗口？','提示',btn,function(e){
  //  if(e.index==0){
  	//	plus.webview.currentWebview().close();
	    	var indexPage = plus.webview.getLaunchWebview();
	    	plus.storage.setItem(KeyCfg.currClickTabId,"userAccount");
	    indexPage.show();
	    mui.fire(indexPage,'refresh',{});
//    }
 // });
}
mui.plusReady(function() {
	
	function activeTabItem(tabItemId){
		if(tabItemId == "defaultTab") return;/*默认选中的是第一个选项卡*/
		var tabItem = $("#"+tabItemId);
		tabItem.addClass("mui-active");
		var href = tabItem.attr("href");
	    $(href).show();
	    $("#defaultTab").removeClass("mui-active");
	    $("#item1mobile").hide();	
	}
	var activeTabItemId = plus.webview.currentWebview().activeTabItemId;
	if(!activeTabItemId) activeTabItemId = "defaultTab";
	//activeTabItem(activeTabItemId);
	var defaultTab = document.getElementById(activeTabItemId);
	//模拟首页点击
	mui.trigger(defaultTab,'tap');
	
	/*-------  竞标的投资 CODE START -------*/
	var doing_url = UrlConfig.minvestRecord_initTwo;
	$.ajax(doing_url, {  
        data : {},  
        dataType : 'jsonp',  
        crossDomain: true,  
        success: function(data) {
        		data = JSON.parse(data);
        		callback(data);
        }
    });
    
    function callback(data){
    		//count,count_money
    		var count = data.count;
    		var count_money = data.count_money;
    		var loanRecords = data.loanRecords;
    		if(!loanRecords || loanRecords.length == 0){
    			$("#item1_sum_header").hide();
    			return;
    		}
    		$("#item1_sum_amount").html(count_money);
    		$("#item1_sum_hilight").html(count);
    		var htmlArr = [];
    		for(var i=0,len=loanRecords.length; i<len; i++){
    			var loanRecord = loanRecords[i];
    			htmlArr[i] = getJbHtml(loanRecord);
    		}
    		var htmlStr = htmlArr.join(" ");
    		$("#item1_loanRecords").html(htmlStr);
    }
    
    function getJbHtml(loanRecord){
		var loanRecorId = loanRecord.loanRecorId;
		var loanSignId = loanRecord.loanSignId;
		var loanTitle = loanRecord.loanTitle;
		var fabuTime = loanRecord.fabuTime;
		var borrName = loanRecord.borrName;
		var tenderMoney = loanRecord.tenderMoney;
		var sumPiamount = loanRecord.sumPiamount;
    		var htmlArr = [
    			'<li class="mui-table-view-cell">',
			'	<div>',
			'		<div><span class="cell_label">借款标号：</span>'+loanTitle+'</div>',
			'		<div><span class="cell_label">借款人：</span>'+borrName+'</div>',
			'		<div><span class="cell_label">发标日期：</span>'+fabuTime+'</div>',
			'		<div><span class="cell_label">投资金额：</span>￥'+tenderMoney+'</div>',
			'		<div><span class="cell_label">预期本息：</span>￥'+sumPiamount+'</div>',
			'	</div>',
			'</li>'
			];
		return htmlArr.join(" ");
    }/*-------  竞标的投资 CODE END -------*/
   
   /*-------  回款的投资 CODE START -------*/
  	var buy_url = UrlConfig.minvestRecord_initThree;
	$.ajax(buy_url, {  
        data : {},  
        dataType : 'jsonp',  
        crossDomain: true,  
        success: function(data) {
        		data = JSON.parse(data);
        		callback3(data);
        }
    });
    
    function callback3(data){
    		//count,count_money
    		var count = data.count;
    		var count_money = data.count_money;
    		var prepepayDate = data.prepepayDate;
    		var loanRecords = data.loanRecords;
    		if(!loanRecords || loanRecords.length == 0){
    			$("#item2_sum_header").hide();
    			return;
    		}
    		$("#item2_sum_amount").html(count_money);
    		$("#item2_sum_hilight").html(count);
    		$("#item2_date").html(prepepayDate);
    		var htmlArr = [];
    		for(var i=0,len=loanRecords.length; i<len; i++){
    			var loanRecord = loanRecords[i];
    			htmlArr[i] = getJbHtml3(loanRecord);
    		}
    		var htmlStr = htmlArr.join(" ");
    		$("#item2_loanRecords").html(htmlStr);
    }
    
    function getJbHtml3(loanRecord){
		var loanRecorId = loanRecord.loanRecorId;
		var loanSignId = loanRecord.loanSignId;
		var loanTitle = loanRecord.loanTitle;
		var tenderTime = loanRecord.tenderTime;
		var borrName = loanRecord.borrName;
		var tenderMoney = loanRecord.tenderMoney;
		var refundDeadline = loanRecord.refundDeadline;
    		var htmlArr = [
    			'<li class="mui-table-view-cell">',
			'	<div>',
			'		<div><span class="cell_label">借款标号：</span>'+loanTitle+'</div>',
			'		<div><span class="cell_label">借款人：</span>'+borrName+'</div>',
			'		<div><span class="cell_label">投资日期：</span>'+tenderTime+'</div>',
			'		<div><span class="cell_label">投资金额：</span>￥'+tenderMoney+'</div>',
			'		<div><span class="cell_label">期数(已还)：</span>'+refundDeadline+'</div>',
			'	</div>',
			'</li>'
			];
		return htmlArr.join(" ");
    }/*-------  回款的投资 CODE END -------*/
   
    /*-------  已完成的投资 CODE START -------*/
  	var finish_url = UrlConfig.minvestRecord_initFour;
	$.ajax(finish_url, {  
        data : {},  
        dataType : 'jsonp',  
        crossDomain: true,  
        success: function(data) {
        		data = JSON.parse(data);
        		callback4(data);
        }
    });
    
    function callback4(data){
    		var loanRecords = data.loanRecords;
    		if(!loanRecords || loanRecords.length == 0) return;
    		var htmlArr = [];
    		for(var i=0,len=loanRecords.length; i<len; i++){
    			var loanRecord = loanRecords[i];
    			htmlArr[i] = getJbHtml4(loanRecord);
    		}
    		var htmlStr = htmlArr.join(" ");
    		$("#item3_loanRecords").html(htmlStr);
    }
    
    function getJbHtml4(loanRecord){
		var loanRecorId = loanRecord.loanRecorId;
		var loanSignId = loanRecord.loanSignId;
		var loanTitle = loanRecord.loanTitle;
		var tenderTime = loanRecord.tenderTime;
		var borrName = loanRecord.borrName;
		var tenderMoney = loanRecord.tenderMoney;
		var refundDeadline = loanRecord.refundDeadline;
		var tzyj = loanRecord.tzyj;
    		var htmlArr = [
    			'<li class="mui-table-view-cell">',
			'	<div>',
			'		<div><span class="cell_label">借款标号：</span>'+loanTitle+'</div>',
			'		<div><span class="cell_label">借款人：</span>'+borrName+'</div>',
			'		<div><span class="cell_label">投资日期：</span>'+tenderTime+'</div>',
			'		<div><span class="cell_label">投资金额：</span>￥'+tenderMoney+'</div>',
			'		<div><span class="cell_label">管理费：</span>￥'+tzyj+'</div>',
			'		<div><span class="cell_label">期数(已还)：</span>'+refundDeadline+'</div>',
			'	</div>',
			'</li>'
			];
		return htmlArr.join(" ");
    }/*-------  已完成的投资 CODE END -------*/
   
   
    /*-------  已赎回的投资 CODE START -------*/
  	var back_url = UrlConfig.minvestRecord_initFive;
	$.ajax(back_url, {  
        data : {},  
        dataType : 'jsonp',  
        crossDomain: true,  
        success: function(data) {
        		data = JSON.parse(data);
        		callback5(data);
        }
    });
    
    function callback5(data){
    		var loanRecords = data.loanRecords;
    		if(!loanRecords || loanRecords.length == 0) return;
    		var htmlArr = [];
    		for(var i=0,len=loanRecords.length; i<len; i++){
    			var loanRecord = loanRecords[i];
    			htmlArr[i] = getJbHtml5(loanRecord);
    		}
    		var htmlStr = htmlArr.join(" ");
    		$("#item4_loanRecords").html(htmlStr);
    }
    
    function getJbHtml5(loanRecord){
		var loanRecorId = loanRecord.loanRecorId;
		var loanSignId = loanRecord.loanSignId;
		var loanTitle = loanRecord.loanTitle;
		var tenderTime = loanRecord.tenderTime;
		var borrName = loanRecord.borrName;
		var tenderMoney = loanRecord.tenderMoney;
		var refundDeadline = loanRecord.refundDeadline;
		var tzyj = loanRecord.tzyj;
    		var htmlArr = [
    			'<li class="mui-table-view-cell">',
			'	<div>',
			'		<div><span class="cell_label">借款标号：</span>'+loanTitle+'</div>',
			'		<div><span class="cell_label">借款人：</span>'+borrName+'</div>',
			'		<div><span class="cell_label">投资日期：</span>'+tenderTime+'</div>',
			'		<div><span class="cell_label">投资金额：</span>￥'+tenderMoney+'</div>',
			'		<div><span class="cell_label">期数(已还)：</span>'+refundDeadline+'</div>',
			'	</div>',
			'</li>'
			];
		return htmlArr.join(" ");
    }/*-------  已赎回的投资 CODE END -------*/
   
});